import json


from common import errors
from lib.http import render_json
from lib.util import get_now_time
from user.models import User
from logs.models import Log, LinkLog, convert_us_to_china_time

from django.db.models import Q
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# 查询日志数据接口
def get_logs(request):
    try:
        pass #print(request.body)
        data = json.loads(request.body)
    except Exception as e:
        # raise errors.ParamsError
        pass
    # 获取当前用户id
    user_id = 1

    # 获取参数信息
    LogType = data.get('LogType')
    LogOption = data.get('LogOption')
    LogResult = data.get('LogResult')
    LogTime = data.get('LogTime')
    filters = Q()
    if user_id != 1:
        filters &= Q(UserId=user_id)
    if LogType:
        filters &= Q(LogType=LogType)
    if LogOption:
        filters &= Q(LogOption=LogOption)
    if LogResult:
        filters &= Q(LogResult=LogResult)
    # if LogTime:
    #     filters &= Q(LogStartTime__lte=LogTime, LogEndTime__gte=LogTime)
    logs_query = Log.objects.filter(filters).order_by("-LogId")
    logs = [
        {
            'LogId': log.LogId,
            'UserId': log.UserId,
            'LogType': log.LogType,
            'LogOption': log.LogOption,
            'LogOptionDetail': log.LogOptionDetail,
            'LogResult': log.LogResult,
            'LogStartTime': log.LogStartTime.isoformat() if log.LogStartTime else None,
            'LogEndTime': log.LogEndTime.isoformat() if log.LogEndTime else None,
        }
        for log in logs_query
    ]
    # 分页
    pass #print(logs_query)
    return render_json(data={"data": logs})


def get_link_logs(request):
    try:
        pass #print(request.body)
        data = json.loads(request.body)
    except Exception as e:
        # raise errors.ParamsError
        pass
    # 获取当前用户id
    user_id = 1

    # 获取参数信息
    LinkID = data.get('id')
    filters = Q()
    filters &= Q(LinkID=LinkID)
    # if LogTime:
    #     filters &= Q(LogStartTime__lte=LogTime, LogEndTime__gte=LogTime)
    logs_query = LinkLog.objects.filter(filters).order_by("-LogStartTime")
    link_logs = [
        # {
        #     "id": log.LogId,
        #     'LinkLog': log.LogId,
        #     'LogResult': log.LogResult,
        #     'LogStartTime': convert_us_to_china_time(log.LogStartTime.isoformat()) if log.LogStartTime else None,
        #     'LogEndTime': convert_us_to_china_time(log.LogEndTime.isoformat()) if log.LogEndTime else None,
        # }
        log.to_json()
        for log in logs_query
    ]
    # 分页
    pass #print(logs_query)
    return render_json(data={"data": link_logs})

def get_log_detail(request):
    try:
        pass #print(request.body)
        data = json.loads(request.body)
    except Exception as e:
        # raise errors.ParamsError
        pass

    # 获取参数信息
    LogId = data.get('id')
    filters = Q()
    filters &= Q(LogId=LogId)
    # if LogTime:
    #     filters &= Q(LogStartTime__lte=LogTime, LogEndTime__gte=LogTime)
    logs_query = LinkLog.objects.filter(filters)
    link_logs = [
        # {
        #     "id": log.LogId,
        #     'LinkLog': log.LogId,
        #     'LogResult': log.LogResult,
        #     'Logdetails': log.LogOptionDetail,
        #     'LogStartTime': convert_us_to_china_time(log.LogStartTime.isoformat()) if log.LogStartTime else None,
        #     'LogEndTime': convert_us_to_china_time(log.LogEndTime.isoformat()) if log.LogEndTime else None,
        # }
        log.to_json()
        for log in logs_query
    ]
    # 分页
    pass #print(link_logs)
    return render_json(data={"data": link_logs})